<template>
    <div class="login" style="overflow: hidden">
        <div class="login_box">
            <div class="illustration">
                <img src="./../../assets/images/login.png" alt="" />
            </div>
            <div class="input_box">
                <div class="box_title">
                    <p></p>
                    <div class="proname">互联网医院 管理端</div>
                    <p></p>
                </div>
                <div class="tabs_login_box">
                    <div @click="onSwitchLoginType('password')" class="tab_item" :class="{'active': loginType == 'password'}">户名密码登录</div>
                    <div class="divider-line"></div>
                    <div @click="onSwitchLoginType('ca')" class="tab_item" :class="{'active': loginType == 'ca'}">CA登录</div>
                </div>
                <template v-if="loginType == 'password'">
                <div class="login_name">
                    <img src="./../../assets/images/user.png" alt="" />
                    <input
                        type="text"
                        @change="checkInput"
                        v-model.trim="userName"
                        placeholder="请输入账号"
                        ref="userName"
                        clearable
                        @keyup="userName = userName.replace(/[^\w_]/g, '')"
                        class="input1"
                    />
                </div>
                <div class="line"></div>
                <div class="login_password">
                    <img src="./../../assets/images/psw.png" alt="" />
                    <input
                        autocomplete="new-password"
                        type="password"
                        @change="checkInput"
                        v-model.trim="password"
                        placeholder="请输入密码"
                        clearable
                        class="input1"
                    />
                </div>
                <div class="line"></div>
                <div class="login_password">
                    <img src="./../../assets/images/verify.png" alt="" />
                    <div class="verify-in-box">
                        <input
                            type="text"
                            class="verify"
                            v-model="value2Computed"
                            placeholder="请输入验证码"
                            @keyup.enter="checkLogin"
                            @keyup="verify = verify.replace(/[^\w_]/g, '')"
                            clearable
                        />
                        <div
                            class="typecode"
                            :class="{
                                hdr: fullHeight < 900,
                                odr: fullHeight > 900
                            }"
                            @click="changeCode"
                        >
                            &nbsp;{{ identifyCode }}
                        </div>
                    </div>
                </div>
                <div class="login_btn" @click="checkLogin">登录</div>
                </template>
                <template v-if="loginType == 'ca'">
                <div class="ca_val">
                    <Select :value="caUserCertId" @on-change="onChangeCaUserCert"  placeholder="选择CA证书">
                        <Option 
                            v-for="item in userCertList"
                            :key="item.value"
                            :value="item.value"
                        >
                            {{ item.name }}
                        </Option>
                    </Select>
                </div>
                <div class="line"></div>
                <div class="login_password">
                    <img src="./../../assets/images/ca.png" alt="" />
                    <input
                        type="password"
                        v-model="caUserPassword"
                        placeholder="请输入PIN码"
                        class="input1"
                    />
                </div>
                <div class="line"></div>
                <p class="ca_login_text">
                    请下载并安装 <a>CA数字证书驱动</a>。<br>
                    默认PIN“111111”，可通过驱,动程序提供的PIN码修改工具进行修改。<br>
                </p>
                <div class="login_btn" @click="caLogin">登录</div>
                </template>
            </div>
        </div>
        <div class="support">
            CopyRight@2024&nbsp;&nbsp;
        </div>
        <div class="authentication" v-show="modalStatus">
            <div class="auth_box">
                <img
                    src="./../../assets/images/close2.png"
                    @click="modalStatus = false"
                    class="close"
                />
                <div class="auth_phone">
                    <img src="./../../assets/images/phone.png" alt="" />
                    <input
                        type="text"
                        v-model="formValidate.phone"
                        placeholder="手机号码"
                        disabled
                        class="input2 blue"
                    />
                </div>
                <div class="line"></div>
                <div class="auth_password">
                    <img src="./../../assets/images/verify.png" alt="" />
                    <!-- type="password" -->
                    <input
                        type="text"
                        v-model="formValidate.sendSMS"
                        placeholder="请输入手机验证码"
                        class="input2"
                        pattern="[0-9]*"
                        maxlength="6"
                    />
                    <div class="send" @click="sendEvent">
                        <span v-if="sendMsg">{{ second }}</span
                        >{{ sendText }}
                    </div>
                </div>
                <div class="auth_btn" @click="phoneLogin">提交</div>
            </div>
        </div>
    </div>
</template>
<script>
require('@/plugins/gVerify.js');
require('@/plugins/jquery-2.1.1.min.js');
import config from '@/configs'
import api from '@/api/commonApi';
import axios from 'axios';
import cookie from '../../utils/cookie.js';
import { Encrypt, Decrypt } from '../../plugins/aes.js';
import Vue from 'vue';
export default {
    data() {
        const validateTypecode = (rule, value, callback) => {
            if (!value) {
                callback(new Error('请输入验证码'));
            } else {
                if (value !== this.identifyCode) {
                    callback(new Error('验证码不一致'));
                } else {
                    callback();
                }
            }
        };
        return {
            isUsePlan: 0,
            workOrderList: [
                {
                    label: '是',
                    value: '1'
                },
                {
                    label: '否',
                    value: '0'
                }
            ],
            second: 60,
            sendMsg: false,
            sendText: '发送验证码',
            identifyCode: 'BC78',
            identifyCodes: 'ABCDEFGHJKMNPQRSTWXYZ0123456789',
            fullHeight: document.documentElement.clientHeight,
            operateUserId: null,
            noticeClassColor: '',
            iconClass: '',
            iconText: '',
            alertMsg: '',
            modalStatus: false,
            userName: "",                     //adminxk
            password: "",               //admin1234
            verify: "",
            verifyCode: null,
            loginFlag: true,
            formValidate: {
                phone: '',
                sendSMS: ''
            },
            ruleValidate: {
                sendSMS: [
                    {
                        required: true,
                        message: '请输入登录验证码',
                        trigger: 'blur'
                    }
                ]
            },
            api: '/api', //gateway的路由
            loginType: 'password',
            userCertList: [],
            caUserCertId: '',
            caUserPassword: '',
            caUserCerBase64: '',
            caUid: ''
        };
    },
    computed: {
        value2Computed: {
            get: function () {
                return this.verify;
            },
            set: function (val) {
                this.verify = val.toUpperCase();
            }
        }
    },
    watch: {
        fullHeight(val) {
            if (!this.timer) {
                this.fullHeight = val;
                this.timer = true;
                let that = this;
                setTimeout(function () {
                    that.timer = false;
                }, 400);
            }
        }
    },
    destroyed() {
        window.removeEventListener('beforeunload', (e) =>
            this.beforeunloadHandler(e)
        );
        window.removeEventListener('unload', (e) => this.unloadHandler(e));
    },
    mounted() {

        const that = this;
        window.addEventListener('beforeunload', (e) =>
            this.beforeunloadHandler(e)
        );
        window.addEventListener('unload', (e) => this.unloadHandler(e));
        window.onresize = () => {
            return (() => {
                window.fullHeight = document.documentElement.clientHeight;
                that.fullHeight = window.fullHeight;
            })();
        };
        this.clearData();
        this.changeCode();

        //获取CA用户证书列表
        //this.userCertList = this.$CA.userCertList

    },
    methods: {
        /**
         * 切换登录方式
         * @param {string} type password 密码登录 | ca CA登录
         * */ 
         onSwitchLoginType(type){
            if(type == 'ca'){
                this.$Message.warning('暂未开通CA登录功能，请使用密码登录！'); 
                return
            }
            
            this.loginType = type
        },
        /**
         * 切换CA用户证书
         */
        onChangeCaUserCert(e){
            this.caUserCertId = e
            this.$CA.chooseCert(e)
        },
        /**
         * CA登录
         */
         async caLogin(){
            if(!this.caUserCertId){
                this.$Message.warning('请选择CA证书');
                return;
            }
                
            if(!this.caUserPassword){
                this.$Message.warning('请输入PIN码');
                return;
            }


            //CA APi登录
            try{
                let res = await this.$CA.login(this.caUserPassword)
                window.localStorage.setItem('CA_USER_CERT_ID', res.ctx.userCertId)
            }catch(error){
                this.$Message.error(error.message);
                return
            }

            //获取服务端随机数
            let randomRes
            try{
                let res = await this.$axios.post(api.caServerRandom, {})
                
                if(res.data.code != 1){
                    this.$Message.error(res.data.message);
                    return
                }

                randomRes = res
            }catch(error){
                this.$Message.error(error.message);
                return
            }

            //验证服务端随机数签名
            try{
                let { random, randomSign, serverCert } = randomRes.data.object
                await this.$CA.verifyRemoteSigned(serverCert, random, randomSign)
            }catch(error){
                this.$Message.error(error.message);
                return
            }

            //服务端验证客户端证书有效性
            try{
                let res = await this.$axios.post(api.caServerVerifyUserCert, {userCert: this.$CA.userCertBase64})

                if(res.data.code != 1){
                    this.$Message.error(res.data.message);
                    return
                }
            }catch(error){
                this.$Message.error(error.message);
                return
            }

            //生成客户端随机数签名,并发送给服务端验证
            // try{
            //     let random = new Date().getTime()
            //     let certSignRes = await this.$CA.signData(random)
                
            //     let res = await this.$axios.post(this.$api.caServerVerifyClientSign,{
            //         plain: random,
            //         plainSign: certSignRes.retVal,
            //         userCert: this.$CA.userCertBase64,             
            //     })

            //     if(res.data.code != 1){
            //         this.$message({
            //             message: res.data.message,
            //             type: "error",
            //         });
            //         this.loadingType = false 
            //         return
            //     }
            // }catch(error){
            //     this.$message({
            //         message: error.message,
            //         type: "error",
            //     });
            //     this.loadingType = false
            //     return
            // }

            //服务端验证客户端证书有效性
            try{
                await this.$CA.getUserCertInfo()
                this.checkLogin()
            }catch(error){
                this.$Message.error(error.message);
                return
            }
        },
        //刷新验证码
        changeCode() {
            this.identifyCode = '';
            this.makeCode(this.identifyCodes, 4);
        },
        randomNum(min, max) {
            max = max + 1;
            return Math.floor(Math.random() * (max - min) + min);
        },
        // 随机生成验证码字符串
        makeCode(data, len) {
            for (let i = 0; i < len; i++) {
                this.identifyCode += data[this.randomNum(0, data.length - 1)];
            }
        },
        clickitem(e) {
            if (e === isUsePlan) {
                this.isUsePlan = '';
            } else {
                this.isUsePlan = e;
            }
        },
        handleReset() {
            this.formValidate.sendSMS = '';
            this.modalStatus = false;
        },
        sendEvent() {
            if (!this.sendMsg) {
                //发送验证码
                this.sendMsg = true;
                this.second = 59;
                this.sendText = '秒后重新获取';
                this.countDown();
                this.getcode();
            }
        },
        //倒计时
        countDown() {
            let that = this;
            let timer = setInterval(() => {
                that.second = --that.second;
                if (that.second <= 0 || !this.modalStatus) {
                    that.sendMsg = false;
                    this.sendText = '发送验证码';
                    clearInterval(timer);
                }
            }, 1000);
        },
        //获取验证码
        getcode() {
            let logcaldata = true;
            let body = {
                version: '2',
                uri: api.getSendSMS.replace('{id}', this.operateUserId),
                method: 'POST'
                // body: { id: this.operateUserId },
            };
            body = Encrypt(
                this.$store.state.loginkeytemp,
                this.$store.state.iv,
                JSON.stringify(body)
            );
            axios
                .post(this.api, { data: body, logcaldata: logcaldata })
                .then((res) => {
                    if (res.data.code == 1) {
                        this.$Message.info('验证码发送成功');
                    }
                })
                .catch(function (error) {});
        },
        //手机号验证码登录
        phoneLogin() {
            if (!this.formValidate.sendSMS) {
                this.$Notice.error({
                    title: '请输入验证码'
                });
                return;
            }
            let body = {
                id: this.operateUserId,
                smsCode: this.formValidate.sendSMS.trim()
            };
            let iv = this.$store.state.iv;
            let loginkeytemp = this.$store.state.loginkeytemp;

            let ginseng = {
                version: this.$store.state.version,
                uri: api.sendSMSlogin,
                method: 'POST',
                body
            };
            ginseng = Encrypt(loginkeytemp, iv, JSON.stringify(ginseng));
            let seng = Decrypt(loginkeytemp, iv, ginseng);
            let logcaldata = true;
            if (this.loginFlag) {
                this.loginFlag = false;
                axios
                    .post(this.api, { data: ginseng, logcaldata: logcaldata })
                    .then((resp) => {
                        if (resp.data.code == 1) {
                            this.$Notice.success({
                                title: resp.data.message
                            })

                            let times = 10 * 60 * 60;
                            window.localStorage.setItem(
                                'access_token',
                                resp.data.object.access_token
                            );
                            // 云信初始化
                            cookie.setCookie('uid',resp.data.object.wyAccid);
                            cookie.setCookie('sdktoken',resp.data.object.wyToken);
                            config.appkey = resp.data.object.wyAppKey
		                    this.$store.dispatch("connect");
		                    this.$store.dispatch("updateRefreshState");
                            // this.$store.dispatch("connectNim",{})
                            let tmpIcon = resp.data.object.userIcon;
                            let userName = resp.data.object.nickname;
                            let operateUserId = resp.data.object.operateUserId;
                            let identity = resp.data.object.identity;
                            let identityCoding =
                                resp.data.object.identityCoding;
                            let userIsYq = resp.data.object.userIsYq;
                            let areaCode = resp.data.object.areaCode;
                            let hospitalEntity =
                                resp.data.object.hospitalEntity;
                            if (operateUserId) {
                                let tmpObj;
                                if (tmpIcon) {
                                    tmpObj = JSON.parse(tmpIcon);
                                    let userIcon =
                                        this.fileBaseUrl + tmpObj.fileName;
                                    cookie.setCookie(
                                        'userIcon',
                                        userIcon,
                                        times
                                    );
                                }
                                cookie.setCookie('userName', userName, times);
                                cookie.setCookie(
                                    'operateUserId',
                                    operateUserId,
                                    times
                                );
                                cookie.setCookie('idtt', identity, times);
                                cookie.setCookie(
                                    'idttC',
                                    identityCoding,
                                    times
                                );
                                cookie.setCookie('userIsYq', userIsYq, times);
                                if (hospitalEntity && hospitalEntity.orgCode) {
                                    cookie.setCookie(
                                        'orgCode',
                                        hospitalEntity.orgCode,
                                        times
                                    );
                                }
                                if (hospitalEntity && hospitalEntity.orgName) {
                                    cookie.setCookie(
                                        'orgName',
                                        hospitalEntity.orgName,
                                        times
                                    );
                                }
                                if (hospitalEntity && hospitalEntity.id) {
                                    cookie.setCookie(
                                        'JGhospitalId',
                                        hospitalEntity.id,
                                        times
                                    );
                                }
                                if (
                                    hospitalEntity &&
                                    hospitalEntity.provinceName
                                ) {
                                    cookie.setCookie(
                                        'provinceName',
                                        hospitalEntity.provinceName,
                                        times
                                    );
                                }
                                if (hospitalEntity && hospitalEntity.cityName) {
                                    cookie.setCookie(
                                        'cityName',
                                        hospitalEntity.cityName,
                                        times
                                    );
                                }
                                if (
                                    hospitalEntity &&
                                    hospitalEntity.districtName
                                ) {
                                    cookie.setCookie(
                                        'districtName',
                                        hospitalEntity.districtName,
                                        times
                                    );
                                }
                                cookie.setCookie(
                                    'hospitalEntity',
                                    JSON.stringify(hospitalEntity || {}),
                                    times
                                );
                            }
                            let key = resp.data.object.randmId;
                            let access_user = resp.data.object['access_user'];
                            let iv = this.$store.state.iv;
                            let menus = resp.data.object.menus;
                            cookie.setCookie('randmId', key, times);
                            cookie.setCookie(
                                'access_user',
                                Encrypt(key, iv, access_user),
                                times
                            );
                            //存储当前用户身份对应省市区
                            let province = null,
                                city = null,
                                area = null;
                            switch (parseInt(identity)) {
                                case 1: //超级管理员，暂时不管
                                    break;
                                case 2: //省级
                                    province =
                                        this.$store.getters.getProvinceById(
                                            identityCoding
                                        );
                                    break;
                                case 3: //市级
                                    city =
                                        this.$store.getters.getCityById(
                                            identityCoding
                                        );
                                    province =
                                        this.$store.getters.getProvinceByCityId(
                                            identityCoding
                                        );
                                    break;
                                case 4: //区级
                                    area =
                                        this.$store.getters.getAreaById(
                                            identityCoding
                                        );
                                    city =
                                        this.$store.getters.getCityByAreaId(
                                            identityCoding
                                        );
                                    province =
                                        this.$store.getters.getProvinceByCityId(
                                            city.id
                                        );
                                    break;
                                case 5: //机构
                                    area =
                                        this.$store.getters.getAreaById(
                                            areaCode
                                        );
                                    city =
                                        this.$store.getters.getCityByAreaId(
                                            areaCode
                                        );
                                    province =
                                        this.$store.getters.getProvinceByCityId(
                                            city.id
                                        );
                            }
                            let ownArea = JSON.stringify({
                                province,
                                city,
                                area
                            });
                            cookie.setCookie('ownArea', ownArea, times);
                            let topMenu = [],
                                secondMenu = [],
                                thirdMenu = [];
                            if (menus) {
                                menus.map((el, i) => {
                                    if (el.level == 0) {
                                        let tmpTop = {
                                            id: el.id,
                                            name: el.menuName,
                                            level: el.level
                                        };
                                        topMenu.push(tmpTop);
                                    } else if (el.level == 1) {
                                        let tmpSecond = {
                                            id: el.id,
                                            name: el.menuName,
                                            parentId: el.parentId,
                                            level: el.level
                                        };
                                        secondMenu.push(tmpSecond);
                                    } else if (el.level == 2) {
                                        let tmpThird = {
                                            id: el.id,
                                            name: el.menuName,
                                            parentId: el.parentId,
                                            level: el.level,
                                            webPath: el.webPath
                                        };
                                        thirdMenu.push(tmpThird);
                                    }
                                });
                            } else {
                                this.$Message.info('请先给此账号开通业务');
                            }
                            topMenu = topMenu.reverse();
                            let menuList = [];
                            for (let i = 0; i < topMenu.length; i++) {
                                let tmpMenu = [];
                                let times = 0;
                                for (let j = 0; j < secondMenu.length; j++) {
                                    let tmpObj = {};
                                    if (
                                        secondMenu[j].parentId == topMenu[i].id
                                    ) {
                                        tmpObj.status = false;
                                        tmpObj.id = topMenu[i].id;
                                        tmpObj.level = secondMenu[j].level;
                                        tmpObj.name = secondMenu[j].name;
                                        tmpObj.childLists = [];
                                        for (
                                            let k = 0;
                                            k < thirdMenu.length;
                                            k++
                                        ) {
                                            if (
                                                thirdMenu[k].parentId ==
                                                secondMenu[j].id
                                            ) {
                                                let tmpObjS = {};
                                                tmpObjS.oname =
                                                    secondMenu[j].name;
                                                tmpObjS.id = thirdMenu[k].id;
                                                tmpObjS.level =
                                                    thirdMenu[k].level;
                                                tmpObjS.name =
                                                    thirdMenu[k].name;
                                                tmpObjS.webPath =
                                                    thirdMenu[k].webPath;
                                                tmpObj.childLists.push(tmpObjS);
                                                if (
                                                    secondMenu[j].parentId ==
                                                        topMenu[i].id &&
                                                    times < 1
                                                ) {
                                                    let start =
                                                        this.findStr(
                                                            thirdMenu[k]
                                                                .webPath,
                                                            '/',
                                                            1
                                                        ) + 1;
                                                    let end = this.findStr(
                                                        thirdMenu[k].webPath,
                                                        '/',
                                                        2
                                                    );
                                                    let type = thirdMenu[
                                                        k
                                                    ].webPath.slice(start, end);
                                                    topMenu[i].type = type;
                                                    times++;
                                                }
                                            }
                                        }
                                        tmpMenu.push(tmpObj);
                                    }
                                }
                                tmpMenu = JSON.stringify(tmpMenu);
                                window.localStorage.setItem(
                                    'sun' + topMenu[i].id,
                                    Encrypt(key, iv, tmpMenu)
                                );
                            }
                            topMenu = JSON.stringify(topMenu);
                            window.localStorage.setItem(
                                'top',
                                Encrypt(key, iv, topMenu)
                            );
                            if (menus.length <= 0) {
                                this.$Message.error({
                                    content: '该账号暂无任何权限',
                                    duration: 3
                                });
                                this.functionJS.queryNavgationTo(this, '/home');
                                return;
                            }
                            this.modalStatus = false;
                            // 公用方法
                            this.functionJS.queryNavgationTo(this, '/home');
                            // 清空输入的内容
                            this.resetInput();
                        } else {
                            this.$router.push('/home');
                        }
                    })
                    .catch((err) => {
                        console.log(err);
                        this.alertMsg = '登录失败';
                    });
            }
        },
        checkLogin() {
           

            let body = {};

            if(this.loginType == 'password'){
                if (!this.userName) {
                    this.$Notice.error({
                        title: '请输入登录名'
                    });
                    return;
                }
                if (!this.password) {
                    this.$Notice.error({
                        title: '请输入登录密码'
                    });
                    return;
                }
                if (!this.verify) {
                    this.$Notice.error({
                        title: '请输入验证码'
                    });
                    return;
                }
                if (this.verify.toUpperCase() !== this.identifyCode) {
                    this.$Notice.error({
                        title: '请输入正确的验证码'
                    });
                    this.verify = '';
                    this.changeCode();
                    return;
                }
                if (!this.userName.trim() || !this.password.trim()) {
                    this.noticeClassColor = 'alert-color';
                    this.iconClass = 'alert-icon';
                    this.iconText = '!';
                    this.alertMsg = '请输入用户名和密码';
                    return;
                } else {
                    this.noticeClassColor = 'success-color';
                    this.iconClass = 'success-icon';
                    this.iconText = 'ok';
                    this.alertMsg = '输入正确，登录中...';
                }
                body = {
                    userName: this.userName.trim(),
                    password: this.password.trim()
                };
            }

            if(this.loginType == 'ca'){
                body = {
                    loginType: '2',
                    userName: this.$CA.userUid
                };
            }
        
            let iv = this.$store.state.iv;
            let loginkeytemp = this.$store.state.loginkeytemp;
            // 原生 axios 登录写法
            let data = {
                version: this.$store.state.version,
                uri: api.login,
                method: 'POST',
                body
            };
            console.log('data111', data);
            data = Encrypt(loginkeytemp, iv, JSON.stringify(data));
            
            let logcaldata = true;
            if (this.loginFlag) {
                this.loginFlag = true;
                // this.$axios.post(api, { data: data, logcaldata: logcaldata})
                
                axios
                    .post(this.api, { data: data, logcaldata: logcaldata })
                    .then((resp) => {
                        if (resp.data.code == 1) {
                            window.localStorage.setItem('LOGIN_INFO',JSON.stringify(resp.data.object));
                            let times = 10 * 60 * 60;
                            window.localStorage.setItem(
                                'access_token',
                                resp.data.object.access_token
                            );
                            // 云信初始化
                            cookie.setCookie('uid',resp.data.object.wyAccid);
                            cookie.setCookie('sdktoken',resp.data.object.wyToken);
                            config.appkey = resp.data.object.wyAppKey
		                    this.$store.dispatch("connect");
		                    this.$store.dispatch("updateRefreshState");
                            // this.$store.dispatch("connectNim",{})
                            let tmpIcon = resp.data.object.userIcon;
                            let userName = resp.data.object.nickname;
                            let operateUserId = resp.data.object.operateUserId;
                            let identity = resp.data.object.identity;
                            let identityCoding =
                                resp.data.object.identityCoding;
                            let userIsYq = resp.data.object.userIsYq;
                            let areaCode = resp.data.object.areaCode;
                            let hospitalEntity =
                                resp.data.object.hospitalEntity;
                            //用户所属父级科室
                            let department = resp.data.object.department != undefined ? resp.data.object.department: '';
                            let hospitalId = resp.data.object.hospitalId != undefined ? resp.data.object.hospitalId: '';
                            if (operateUserId) {
                                let tmpObj;
                                if (tmpIcon) {
                                    tmpObj = JSON.parse(tmpIcon);
                                    let userIcon =
                                        this.fileBaseUrl + tmpObj.fileName;
                                    cookie.setCookie(
                                        'userIcon',
                                        userIcon,
                                        times
                                    );
                                }
                                cookie.setCookie('userName', userName, times);
                                cookie.setCookie(
                                    'operateUserId',
                                    operateUserId,
                                    times
                                );
                                cookie.setCookie('idtt', identity, times);
                                cookie.setCookie(
                                    'idttC',
                                    identityCoding,
                                    times
                                );
                                cookie.setCookie('userIsYq', userIsYq, times);
                                if (hospitalEntity && hospitalEntity.orgCode) {
                                    cookie.setCookie(
                                        'orgCode',
                                        hospitalEntity.orgCode,
                                        times
                                    );
                                }
                                if (hospitalEntity && hospitalEntity.orgName) {
                                    cookie.setCookie(
                                        'orgName',
                                        hospitalEntity.orgName,
                                        times
                                    );
                                }
                                if (hospitalEntity && hospitalEntity.id) {
                                    cookie.setCookie(
                                        'JGhospitalId',
                                        hospitalEntity.id,
                                        times
                                    );
                                }
                                if (
                                    hospitalEntity &&
                                    hospitalEntity.provinceName
                                ) {
                                    cookie.setCookie(
                                        'provinceName',
                                        hospitalEntity.provinceName,
                                        times
                                    );
                                }
                                if (hospitalEntity && hospitalEntity.cityName) {
                                    cookie.setCookie(
                                        'cityName',
                                        hospitalEntity.cityName,
                                        times
                                    );
                                }
                                if (
                                    hospitalEntity &&
                                    hospitalEntity.districtName
                                ) {
                                    cookie.setCookie(
                                        'districtName',
                                        hospitalEntity.districtName,
                                        times
                                    );
                                }
                                cookie.setCookie(
                                    'hospitalEntity',
                                    JSON.stringify(hospitalEntity || {}),
                                    times
                                );
                            }
                            cookie.setCookie("department",department,times);
                            // cookie.setCookie("hospitalId",hospitalId,times);
                            let key = resp.data.object.randmId;
                            let access_user = resp.data.object['access_user'];
                            let iv = this.$store.state.iv;
                            let salt = this.$store.state.salt;
                            let menus = resp.data.object.menus;
                            cookie.setCookie('randmId', key, times);
                            cookie.setCookie(
                                'access_user',
                                Encrypt(key, iv, access_user),
                                times
                            );
                            console.log('用户名', userName);
                            //存储当前用户身份对应省市区
                            let province = null,
                                city = null,
                                area = null;
                            switch (parseInt(identity)) {
                                case 1: //超级管理员，暂时不管
                                    break;
                                case 2: //省级
                                    province =
                                        this.$store.getters.getProvinceById(
                                            identityCoding
                                        );
                                    break;
                                case 3: //市级
                                    city =
                                        this.$store.getters.getCityById(
                                            identityCoding
                                        );
                                    province =
                                        this.$store.getters.getProvinceByCityId(
                                            identityCoding
                                        );
                                    break;
                                case 4: //区级
                                    area =
                                        this.$store.getters.getAreaById(
                                            identityCoding
                                        );
                                    city =
                                        this.$store.getters.getCityByAreaId(
                                            identityCoding
                                        );
                                    province =
                                        this.$store.getters.getProvinceByCityId(
                                            city.id
                                        );
                                    break;
                                case 5: //机构
                                    area =
                                        this.$store.getters.getAreaById(
                                            areaCode
                                        );
                                    city =
                                        this.$store.getters.getCityByAreaId(
                                            areaCode
                                        );
                                    province =
                                        this.$store.getters.getProvinceByCityId(
                                            city.id
                                        );
                            }
                            let ownArea = JSON.stringify({
                                province,
                                city,
                                area
                            });
                            cookie.setCookie('ownArea', ownArea, times);
                            let topMenu = [],
                                secondMenu = [],
                                thirdMenu = [];
                            if (menus) {
                                menus.map((el, i) => {
                                    if (el.level == 0) {
                                        let tmpTop = {
                                            id: el.id,
                                            name: el.menuName,
                                            level: el.level
                                        };
                                        topMenu.push(tmpTop);
                                    } else if (el.level == 1) {
                                        let tmpSecond = {
                                            id: el.id,
                                            name: el.menuName,
                                            parentId: el.parentId,
                                            level: el.level
                                        };
                                        secondMenu.push(tmpSecond);
                                    } else if (el.level == 2) {
                                        let tmpThird = {
                                            id: el.id,
                                            name: el.menuName,
                                            parentId: el.parentId,
                                            level: el.level,
                                            webPath: el.webPath
                                        };
                                        thirdMenu.push(tmpThird);
                                    }
                                });
                            } else {
                                this.$Message.info('请先给此账号开通业务');
                            }
                            topMenu = topMenu.reverse();
                            let menuList = [];
                            for (let i = 0; i < topMenu.length; i++) {
                                let tmpMenu = [];
                                let times = 0;
                                for (let j = 0; j < secondMenu.length; j++) {
                                    let tmpObj = {};
                                    if (
                                        secondMenu[j].parentId == topMenu[i].id
                                    ) {
                                        tmpObj.status = false;
                                        tmpObj.id = topMenu[i].id;
                                        tmpObj.level = secondMenu[j].level;
                                        tmpObj.name = secondMenu[j].name;
                                        tmpObj.childLists = [];
                                        for (
                                            let k = 0;
                                            k < thirdMenu.length;
                                            k++
                                        ) {
                                            if (
                                                thirdMenu[k].parentId ==
                                                secondMenu[j].id
                                            ) {
                                                let tmpObjS = {};
                                                tmpObjS.oname =
                                                    secondMenu[j].name;
                                                tmpObjS.id = thirdMenu[k].id;
                                                tmpObjS.level =
                                                    thirdMenu[k].level;
                                                tmpObjS.name =
                                                    thirdMenu[k].name;
                                                tmpObjS.webPath =
                                                    thirdMenu[k].webPath;
                                                tmpObj.childLists.push(tmpObjS);
                                                if (
                                                    secondMenu[j].parentId ==
                                                        topMenu[i].id &&
                                                    times < 1
                                                ) {
                                                    let start =
                                                        this.findStr(
                                                            thirdMenu[k]
                                                                .webPath,
                                                            '/',
                                                            1
                                                        ) + 1;
                                                    let end = this.findStr(
                                                        thirdMenu[k].webPath,
                                                        '/',
                                                        2
                                                    );
                                                    let type = thirdMenu[
                                                        k
                                                    ].webPath.slice(start, end);
                                                    topMenu[i].type = type;
                                                    times++;
                                                }
                                            }
                                        }
                                        tmpMenu.push(tmpObj);
                                    }
                                }
                                tmpMenu = JSON.stringify(tmpMenu);
                                window.localStorage.setItem(
                                    'sun' + topMenu[i].id,
                                    Encrypt(key, iv, tmpMenu)
                                );
                            }
                            topMenu = JSON.stringify(topMenu);
                            window.localStorage.setItem(
                                'top',
                                Encrypt(key, iv, topMenu)
                            );
                            if (menus.length <= 0) {
                                this.$Message.error({
                                    content: '该账号暂无任何权限',
                                    duration: 3
                                });
                                this.functionJS.queryNavgationTo(this, '/home');
                                return;
                            }
                            // 公用方法
                            this.functionJS.queryNavgationTo(this, '/home');
                            // 清空输入的内容
                            this.resetInput();
                        } else if (resp.data.code == 3) {
                            this.functionJS.queryNavgationTo(
                                this,
                                '/changerpass'
                            );
                            cookie.setCookie(
                                'levelp',
                                resp.data.object.password_level
                            );
                        } else if (resp.data.code == 4) {
                            this.formValidate.phone = resp.data.object.phone;
                            this.operateUserId = resp.data.object.operateUserId;
                            this.modalStatus = true;
                            this.resetInput();
                        } else {
                            this.loginFlag = true;
                            this.noticeClassColor = 'alert-color';
                            this.iconClass = 'alert-icon';
                            this.iconText = '!';
                            this.$Notice.error({
                                title: resp.data.message + '请重新输入'
                            });
                            // this.verifyCode.refresh();
                            this.changeCode();
                            this.$refs.userName.focus();
                            this.$router.push('/home');
                            this.verify = '';
                            this.userName = '';
                            this.password = '';
                        }
                    })
                    .catch((err) => {
                        console.log(err);
                        this.loginFlag = true;
                        this.noticeClassColor = 'alert-color';
                        this.iconClass = 'alert-icon';
                        this.iconText = '!';
                        this.alertMsg = '登录失败';
                        // this.verifyCode.refresh();
                        this.changeCode();
                        this.verify = '';
                    });
            }
        },
        checkInput() {
            if (!this.userName || !this.password) {
                this.noticeClassColor = 'alert-color';
                this.iconClass = 'alert-icon';
                this.iconText = '!';
                this.alertMsg = '请输入用户名和密码';
                return;
            } else {
                this.noticeClassColor = '';
                this.iconClass = '';
                this.iconText = '';
                this.alertMsg = '';
            }
        },
        // 输入值重置
        resetInput() {
            // 样式
            this.noticeClassColor = '';
            this.iconClass = '';
            // 符号
            this.iconText = '';
            // 弹示框
            this.alertMsg = '';
            // 刷新验证码
            // this.verifyCode.refresh();
            this.changeCode();
            // 失去焦点
            this.$refs.userName.focus();
            this.verify = '';
            // 用户名密码
            this.userName = '';
            this.password = '';
            // 打开允许登录
            this.loginFlag = true;
        },
        clearData() {
            let assets_token = window.localStorage.getItem('access_token');
            if (assets_token) {
                window.localStorage.removeItem('access_token');
            }
            let cookiess = document.cookie.split(';');
            cookie.delCookie('userName');
            cookie.delCookie('userIcon');
            cookie.delCookie('randmId');
            cookie.delCookie('operateUserId');
            cookie.delCookie('idtt');
            cookie.delCookie('idttC');
            cookie.delCookie('access_user');
            cookie.delCookie('ownArea');
            window.localStorage.removeItem('access_token');
            window.localStorage.removeItem('top');
            window.localStorage.removeItem('sun1');
            window.localStorage.removeItem('sun2');
            window.localStorage.removeItem('sun3');
            window.localStorage.removeItem('sun4');
            window.localStorage.removeItem('sun5');
            // this.resetInput();
        }
    }
};
</script>
<style lang="less" scoped>
.login {
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #c7e9fb, #a6defa, #80d4f9, #5bc9f8, #35bef7);
    background-size: 100% 100%;
    background-repeat: no-repeat;
    position: absolute;
    left: 0;
    display: flex;
    overflow: hidden;
    -webkit-align-items: center;
    align-items: center;
    -webkit-justify-content: center;
    justify-content: center;
    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='login_bg.png',sizingMethod='scale');
    .support {
        font-size: 1.125rem;
        text-align: center;
        position: relative;
        top: 40.5%;
        font-family: PingFang SC;
        font-weight: bold;
        color: #ffffff;
        letter-spacing: 1px;
    }
    input::-webkit-input-placeholder {
        color: #999999;
    }
    .authentication {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 99;
        background: rgba(0, 0, 0, 0.55);
        .auth_box {
            margin: 16% 0 0 34%;
            width: 56vh;
            height: 37vh;
            background: #ffffff;
            border-radius: 35px;
            padding: 2% 0 0 0;
            .close {
                width: 19px;
                height: 18px;
                margin: -16% 0 0 100%;
                cursor: pointer;
                pointer-events: auto;
            }
            .line {
                width: 100%;
                height: 30px;
            }
            .auth_btn {
                width: 21.6vh;
                height: 6vh;
                color: #fff;
                margin: 0 auto;
                font-size: 18px;
                background: #2d6cff;
                line-height: 6vh;
                letter-spacing: 5px;
                cursor: pointer;
                text-align: center;
                margin-top: 9%;
                box-shadow: 0px 9px 21px 0px rgba(45, 108, 255, 0.28);
                border-radius: 30px;
            }
            .auth_phone,
            .auth_password {
                width: 44vh;
                height: 5.9vh;
                line-height: 5.9vh;
                border: 2px solid #2d6cff;
                border-radius: 30px;
                background: #f2f9ff;
                margin: 0 auto;
                padding: 0 0 0 10px;
                img {
                    width: 24px;
                    height: 24px;
                    margin: -8px 0 0 0;
                }
                .input2 {
                    width: 23vh;
                    height: 6vh;
                    background: none;
                    border: none;
                    font-size: 16px;
                    line-height: 45px;
                    outline: none;
                    border: none;
                    text-indent: 10px;
                    margin-bottom: 26px;
                    background: none;
                    border-radius: 3px;
                    color: #333;
                    border-bottom: 1px solid #fff;
                }
                .blue {
                    color: #2d6cff;
                }
                .input3 {
                    width: 13vh;
                    height: 6vh;
                    background: none;
                    border: none;
                    font-size: 16px;
                    line-height: 45px;
                    outline: none;
                    border: none;
                    text-indent: 10px;
                    margin-bottom: 26px;
                    background: none;
                    border-radius: 3px;
                    color: #333;
                    border-bottom: 1px solid #fff;
                }
                .send {
                    width: 12.9vh;
                    height: 4.5vh;
                    line-height: 4.5vh;
                    color: #fff;
                    float: right;
                    font-size: 14px;
                    cursor: pointer;
                    text-align: center;
                    background: #2d6cff;
                    border-radius: 23px;
                    margin: 1% 5px 0 0;
                }
            }
        }
    }
    .login_box {
        width: auto;
        height: 75%;
        position: absolute;
        top: 49%;
        display: flex;
        padding-top: 20px;
        text-align: center;
        transform: translateY(-50%);
        background: rgba(255, 255, 255, 0.48);
        box-shadow: 0px 11px 8px 0px rgba(23, 0, 3, 0.16);
        border-radius: 30px;
        .illustration {
            width: 59vh;
            height: 48vh;
            margin: 15vh 0 0 7vh;
            img {
                width: 59vh;
                height: 48vh;
            }
        }
        .input_box {
            width: 53vh;
            margin: -10% 0 0 0;
            .box_nat {
                width: 53vh;
                height: 38px;
                text-align: center;
                line-height: 38px;
                display: flex;
                margin: 35% 0 14% 5vh;
                .proname {
                    width: 38vh;
                    height: 28px;
                    font-family: PingFang SC;
                    font-weight: 500;
                    color: #2e6cff;
                }
                p:nth-child(1) {
                    width: 6px;
                    height: 6px;
                    background: #138fff;
                    opacity: 0.36;
                    border-radius: 50%;
                    position: relative;
                    top: 50%;
                    left: 5%;
                }
                p:nth-child(2) {
                    width: 10px;
                    height: 10px;
                    background: #8dafff;
                    border-radius: 50%;
                    position: relative;
                    top: 45%;
                    left: 7%;
                }
                p:nth-child(4) {
                    width: 10px;
                    height: 10px;
                    background: #8dafff;
                    border-radius: 50%;
                    position: relative;
                    top: 46%;
                    right: 7%;
                }
                p:nth-child(5) {
                    width: 6px;
                    height: 6px;
                    background: #138fff;
                    opacity: 0.36;
                    border-radius: 50%;
                    position: relative;
                    top: 52%;
                    right: 5%;
                }
            }
            .box_title {
                width: 53vh;
                height: 38px;
                text-align: center;
                line-height: 38px;
                display: flex;
                margin: 35% 0 6% 5vh;
                .proname {
                    width: 38vh;
                    height: 28px;
                    font-family: PingFang SC;
                    font-weight: 500;
                    color: #2e6cff;
                }
                p:nth-child(1) {
                    width: 6px;
                    height: 6px;
                    background: #138fff;
                    opacity: 0.36;
                    border-radius: 50%;
                    position: relative;
                    top: 50%;
                    left: 20%;
                }
                p:nth-child(2) {
                    width: 10px;
                    height: 10px;
                    background: #8dafff;
                    border-radius: 50%;
                    position: relative;
                    top: 45%;
                    left: 22%;
                }
                p:nth-child(4) {
                    width: 10px;
                    height: 10px;
                    background: #8dafff;
                    border-radius: 50%;
                    position: relative;
                    top: 46%;
                    right: 22%;
                }
                p:nth-child(5) {
                    width: 6px;
                    height: 6px;
                    background: #138fff;
                    opacity: 0.36;
                    border-radius: 50%;
                    position: relative;
                    top: 52%;
                    right: 20%;
                }
            }
            input:-internal-autofill-previewed,
            input:-internal-autofill-selected {
                -webkit-text-fill-color: #000;
                transition: background-color 5000s ease-out 0.5s;
            }
            input {
                font-size: 16px;
                line-height: 45px;
                outline: none;
                border: none;
                text-indent: 10px;
                margin-bottom: 26px;
                background: none;
                border-radius: 3px;
                color: #333;
                border-bottom: 1px solid #fff;
            }
            .login_name,
            .login_password,
            .ca_val {
                width: 40.6vh;
                height: 5.93vh;
                line-height: 5.93vh;
                border: 2px solid #2d6cff;
                border-radius: 30px;
                background: #f2f9ff;
                margin: 0 auto;
                img {
                    width: 24px;
                    height: 24px;
                    margin: -8px 0 0 0;
                }
                .input1 {
                    width: 33.3vh;
                    height: 5.93vh;
                    background: none;
                    border: none;
                }
                .verify-in-box {
                    width: 83%;
                    display: inline-flex;
                    justify-content: space-between;
                    .verify {
                        width: 20.3vh;
                        height: 38px;
                        color: #333;
                        text-align: left;
                        border-bottom: 1px solid #f5f5f5;
                    }
                    .typecode {
                        width: 12.4vh;
                        height: 4.5vh;
                        line-height: 4.5vh;
                        color: #fff;
                        cursor: pointer;
                        text-align: center;
                        background: #2d6cff;
                        border-radius: 23px;
                        letter-spacing: 5px;
                    }
                    .hdr {
                        margin: 0.25% -3% 0 0;
                    }
                    .odr {
                        margin: -1.3% -4% 0 0;
                    }
                }
            }
            .ca_val{
                box-sizing: border-box;
                display: flex;
                align-items: center;
                .ivu-select{
                    margin: 0 auto;
                    width: 90%;
                    height: 95%;
                    border: 0 !important;
                }
                /deep/ .ivu-select-placeholder{
                    font-size: 16px;
                    color: #999999;
                }
                /deep/ .ivu-select-selection{
                    width: 100%;
                    height: 100%;
                    border: 0 !important;
                    background: none;
                    outline: none;
                    >div{
                        display: flex;
                        align-items: center;
                        width: 100%;
                        height: 100%;
                        outline: none;
                        border: 0 !important;
                    }
                }
                /deep/ .ivu-select-not-found{
                    font-size: 16px !important;
                }
                /deep/ .ivu-select-item{
                    font-size: 16px !important;
                }
                /deep/ .ivu-select-selected-value{
                    flex: 1;
                    font-size: 16px;
                    color: #666;
                }
                /deep/ .el-input {
                    
                    width: 100%;
                    height: 100%;
                    color: #2d6cff;
                    text-align: center;
                    font-weight: bold;
                    font-size: 16px;
                    input{
                        width: 100%;
                        height: 100%;
                        border: 0;
                        background: none;
                    }
                }
            }
            .ca_login_text{
                padding: 10px;
                box-sizing: border-box;
                width: 40vh;
                line-height: 20px;
                text-align: left;
                border-radius: 10px;
                background: #ffffff;
                margin: 0 auto;
                font-size: 14px;
                color: #333;
            }
            .line {
                width: 100%;
                height: 30px;
            }
            .login_btn {
                width: 40.9vh;
                height: 5.93vh;
                color: #fff;
                margin: 0 auto;
                font-size: 18px;
                background: #2d6cff;
                line-height: 5.93vh;
                letter-spacing: 5px;
                cursor: pointer;
                margin-top: 50px;
                box-shadow: 0px 9px 21px 0px rgba(45, 108, 255, 0.28);
                border-radius: 30px;
            }

            .tabs_login_box{
                margin: 1% auto;
                display: flex;
                align-items: center;
                justify-content: center;
                .divider-line{
                    margin: 0 10px;
                    background-color: #83c5ff;
                    width: 1px;
                    height: 16px;
                }
                .tab_item.active{
                    color: #138fff;
                }
                .tab_item{
                    padding: 10px 0;
                    width: 120px;
                    font-size: 18px;
                    color: #60b5ff;
                    text-align: right;
                    cursor: pointer;
                    font-weight: bold;
                }
                .tab_item:last-child{
                    text-align: left;
                }
            }
        }
    }
    @media screen and (min-device-width: 1024px) and (max-device-width: 2160px) {
        .proname {
            font-size: 1.875rem;
        }
        .typecode {
            font-size: 1.125rem;
        }
        .hdr {
            margin: 0.25% -3% 0 0 !important;
        }
        .input_box {
            margin: -5% 0 0 0 !important;
        }
    }
    @media screen and (max-width: 800px) {
        .proname {
            font-size: 1.5rem;
        }
        .typecode {
            font-size: 14px;
        }
        .hdr {
            margin: 1% -2% 0 0 !important;
        }
        .input_box {
            margin: -10% 0 0 0;
        }
    }
}
</style>
